package com.ssm.pre.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssm.pre.dao.FamilyDao;
import com.ssm.pre.service.FamilyService;
import com.ssm.pre.util.PageCount;
import com.ssm.pre.util.TemplateUtil;
import com.ssm.util.Constants;
import com.ssm.util.object.InputObject;
import com.ssm.util.object.OutputObject;
import com.ssm.wechat.dao.WechatUserDao;
import com.ssm.wechat.service.GetWechatOpenIdByCode;

@Service
public class FamilyServiceImpl implements FamilyService {
	
	@Autowired
	private FamilyDao fDao;
	
	@Autowired
	private WechatUserDao wDao;

	/**
	 * 查询住户信息
	 */
	@Override
	public void queryFamilyList(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		List<Map<String, Object>> beans = fDao.queryFamilyList(map);
		int page = Integer.parseInt(map.get("offset").toString())/Integer.parseInt(map.get("limit").toString());
		page++;
		List<Map<String, Object>> params = PageCount.page(beans, page, Integer.parseInt(map.get("limit").toString()));
		outputObject.setBeans(params);
		outputObject.settotal(beans.size());
	}

	@Override
	public void addFamily(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		int num = fDao.addFamily(map);
		outputObject.settotal(num);
	}

	@Override
	public void editFamily(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> params = inputObject.getParams();
		String openId = params.get("openId").toString();
		if(params.get("familyType").toString().equals("审核通过")){
			fDao.editFamily(params);
			params.put("openId", openId);//不对openID重新赋值，openId为空
			//发送模板消息
			TemplateUtil.Template(params);
			
			//更新wechat_users表的family_id，实现微信信息与family信息的绑定
			wDao.updateWechatUserByFamily(params);
			
		}else if(params.get("familyType").toString().equals("审核失败")){
			fDao.editFamily(params);
			params.put("openId", openId);
			//发送模板消息
			TemplateUtil.Template(params);
			//更新wechat_users表的family_id,重置为0
			params.put("id", 0);
			wDao.updateWechatUserByFamily(params);
			
		}else if(params.get("familyType").toString().equals("未审核")){
			fDao.editFamily(params);
			params.put("openId", openId);
			//发送模板消息
			TemplateUtil.Template(params);
			//更新wechat_users表的family_id,重置为0
			params.put("id", 0);
			wDao.updateWechatUserByFamily(params);
			
		}else{
			outputObject.setreturnMessage("参数错误.");
		}

	}
	/**
	 * 删除住户信息
	 */
	@Override
	public void deleteFamilyById(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> params = inputObject.getParams();
		int num = fDao.deleteFamilyById(params);
		outputObject.settotal(num);

	}

	@Override
	public void queryFamilyOneById(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		Map<String, Object> bean =fDao.findFamilyById(map);
		
		outputObject.setBean(bean);

	}
	

	/**
	 * 用户注册
	 */
	@Override
	public void addWechatFamily(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> map = inputObject.getParams();
		if(map.get("openId").toString().equals("undefined") || map.get("openId").toString().equals("")){//获取openid
			Map<String, Object>	bean =	GetWechatOpenIdByCode.getRequest(Constants.APP_ID, Constants.APPSECRET, (String) map.get("codes"));
			if(bean == null){
				outputObject.setreturnMessage("微信信息为空");
				return;
			}
			map.put("openId", bean.get("openid"));
		}
		map.put("familyType", "未审核");
		//根据openId查找住户是否已经注册
		Map<String, Object> params = fDao.queryWechatFamilyByOpenId(map);
		
		if (params==null) {
			fDao.addWechatFamily(map);
			outputObject.setBean(map);
			//发送模板消息
			TemplateUtil.Template(map);
		}else {
			outputObject.setreturnMessage("该用户已经登记!");
		}
	}
	/**
	 * 获取openId，查询family表是否已存在用户信息,查询当前用户的审核状态
	 */
	@Override
	public synchronized void getWechatOpenId(InputObject inputObject, OutputObject outputObject) throws Exception {
		//获取session信息
		Map<String,Object> map = inputObject.getWechatLogParams();
		if(map != null){//session不为空
			
			//根据sessoin中的openID查询当前的用户状态
			List<Map<String, Object>> familyList = fDao.findWechatFamilyByOpenId(map);
			if(familyList.size() == 0){
				outputObject.setreturnCode("-999");
				outputObject.setBean(map);
			}else{
				if(familyList.get(0) == null){//重新登记
					outputObject.setreturnCode("-999");
					outputObject.setBean(map);
				}else{
					if(familyList.get(0).get("familyType").toString().equals("未审核")){
						outputObject.setreturnCode("-888");
						outputObject.setBean(familyList.get(0));
					}else if(familyList.get(0).get("familyType").toString().equals("审核通过")){
						System.out.println(familyList.get(0));
						outputObject.setBean(familyList.get(0));
					}else{//审核失败
						outputObject.setreturnCode("-777");
						outputObject.setBean(familyList.get(0));
					}
				}
				//存放到session中
				outputObject.setWechatLogParams(familyList.get(0));
			}
			
		}else{//session为空
			//获取code
			Map<String ,Object> params = inputObject.getParams();
			//根据code获取openID
			Map<String, Object>	bean =	GetWechatOpenIdByCode.getRequest(Constants.APP_ID, Constants.APPSECRET, (String) params.get("codes"));
			
			if(bean == null){
				outputObject.setreturnMessage("微信为空");
				return;
			}
			bean.put("openId", bean.get("openid"));
			//bean不为空，查询family表中的信息
			List<Map<String, Object>> familyList = fDao.findWechatFamilyByOpenId(bean);
			if(familyList.size() != 0){
				if(familyList.get(0) != null){
					if(familyList.get(0).get("familyType").toString().equals("未审核")){
						//未审核(正在审核中），不能进行提交操作
						outputObject.setreturnCode("-888");
						outputObject.setBean(familyList.get(0));
					}else if(familyList.get(0).get("familyType").toString().equals("审核通过")){
						outputObject.setBean(familyList.get(0));
					}else{//审核失败，需要重新登记
						outputObject.setreturnCode("-777");
						outputObject.setBean(familyList.get(0));
					}
					outputObject.setWechatLogParams(familyList.get(0));
				}else{
					outputObject.setBean(bean);
					outputObject.setreturnCode("-999");
				}
			}else{
				outputObject.setBean(bean);
				outputObject.setreturnCode("-999");
			}
			
			
			
		}
		
	}

	/**
	 * 获取session中的信息
	 */
	@Override
	public void queryFamilyBySession(InputObject inputObject, OutputObject outputObject) throws Exception {
		Map<String, Object> params = inputObject.getParams();
		Map<String, Object> wechatParams = inputObject.getWechatLogParams();
		
		
		if(wechatParams != null){//session不为空
			outputObject.setreturnMessage(wechatParams);
			outputObject.setWechatLogParams(params);//将code值存入session中
		}else{
			Map<String, Object>	bean =	GetWechatOpenIdByCode.getRequest(Constants.APP_ID, Constants.APPSECRET,params.get("codes").toString());
			if(bean == null){
				outputObject.setreturnMessage("信息为空");
				return;
			}
		}
		
	}


}
